package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketCommentExample;
import com.cskaoyan.bean.po.MarketComment;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.chaosbean.vxorder.bo.CommentBO;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.service.adminservice.CommentService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author dengqwe
 * @datetime 2022/12/30 14:33
 * @description: TODO:
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    MarketCommentMapper commentMapper;

    @Override
    //CommonData<MarketComment>
    public CommonData<MarketComment> list(BasePageInfo info, String userId, String valueId) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCommentExample example = new MarketCommentExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketCommentExample.Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtil.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.valueOf(valueId));
        }
        criteria.andDeletedEqualTo(false);
        List<MarketComment> list = commentMapper.selectByExample(example);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
//        int pageNum = pageInfo.getPageNum();//page
//        int pageSize = pageInfo.getPageSize();//limit
//        int pages = pageInfo.getPages();//pages
//        long total = pageInfo.getTotal();//total
//
//
//
//        List<MarketComment> list1 = pageInfo.getList();
//        CommonData<MarketComment> data = new CommonData<>();
//        data.setPage(pageNum);
//        data.setLimit(pageSize);
//        data.setPages(pages);
//        data.setTotal((int) total);
//        data.setList(list1);
        //封装成一个CommonData对象data

        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public void delete(MarketComment comment) {
        Integer id = comment.getId();
        Boolean deleted = comment.getDeleted();
        MarketComment newComment = new MarketComment();
        newComment.setId(id);
        newComment.setDeleted(!deleted);
        commentMapper.updateByPrimaryKeySelective(newComment);
    }

    @Override
    public void addComment(CommentBO comBO) {
        Date date = new Date();
        MarketComment comment = new MarketComment(null, 1, 0, comBO.getContent(), null, 1, comBO.getHasPicture(), comBO.getPicUrls(), comBO.getStar(), date, date, false);

        commentMapper.insertSelective(comment);
        Integer id = comment.getId();
        System.out.println("id = " + id);
        commentMapper.updateByIdComment(comBO.getOrderGoodsId(),id);

    }
}
